﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.UserModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{
    public partial class UserServiceImpl
    {
        /// <summary>
        /// 获取UserGroup分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<UserGroupDTO> GetUserGroupPaged(PagingFeature pf)
        {
            var paged = _userGroupRepository.Pagging(pf);

            return paged.To<UserGroup, UserGroupDTO>();
        }

        /// <summary>
        ///     根据用户组的唯一ID，得到用户组传输对象
        /// </summary>
        /// <param name="id">用户组唯一标识</param>
        /// <returns></returns>
        public UserGroupDTO GetUserGroupByKey(Guid id)
        {
            var usergroup = _userGroupRepository.Get(id);
            var userGroupDto = usergroup.To<UserGroupDTO>();

            return userGroupDto;
        }

        /// <summary>
        ///     新增用户组
        /// </summary>
        /// <param name="userGroupDto">用户组对象</param>
        public void CreateUserGroup(UserGroupDTO userGroupDto)
        {
            if (userGroupDto == null)
                throw new ArgumentNullException("userGroupDto");

            var usergroup = userGroupDto.To<UserGroup>();


            _userGroupRepository.Add(usergroup);

            Context.Commit();
        }

        /// <summary>
        ///     更新用户组传输对象
        /// </summary>
        /// <param name="userGroupDto">用户组对象</param>
        /// <returns>用户组对象结果集</returns>
        public Result UpdateUserGroup(UserGroupDTO userGroupDto)
        {
            var res = new Result();

            var persisted = _userGroupRepository.Get(userGroupDto.Id);

            persisted.ModifiTime = DateTime.Now;
            persisted.Remark = userGroupDto.Remark;
            persisted.Title = userGroupDto.Title;

            _userGroupRepository.Update(persisted);
            Context.Commit();
            return res;

        }

        /// <summary>
        ///     更新用户组传输对象
        /// </summary>
        /// <param name="userGroupDto">用户组对象</param>
        /// <returns>用户组对象结果集</returns>
        [Logging("保存用户组，{@p0.Title}")]
        public Result SaveUserGroup(UserGroupDTO userGroupDto)
        {
            var res = new Result();
            if (userGroupDto.Id.IsEmpty())
            {
                var usergroup = userGroupDto.To<UserGroup>();


                _userGroupRepository.Add(usergroup);
            }
            else
            {
                var persisted = _userGroupRepository.Get(userGroupDto.Id);

                persisted.ModifiTime = DateTime.Now;
                persisted.Remark = userGroupDto.Remark;
                persisted.Title = userGroupDto.Title;

                _userGroupRepository.Update(persisted);
            }
            Context.Commit();
            return res;

        }
        /// <summary>
        ///     删除用户组(多条)
        /// </summary>
        /// <param name="userGroupids">用户组ID(多条)</param>
        /// <returns>用户组结果集</returns>
        public Result DeleteUserGroups(IList<Guid> userGroupids)
        {
            var res = new Result();
            if (userGroupids == null)
            {
                res.Err = "用户组Id参数不可为空！";
                return res;
            }

            _userGroupRepository.Remove(userGroupids);

            Context.Commit();

            return res;
        }
        /// <summary>
        /// 获取所有用户组
        /// </summary>
        /// <returns></returns>
        public Result<IEnumerable<UserGroupDTO>> GetUserGroups()
        {
            var res = new Result<IEnumerable<UserGroupDTO>>();

            res.Data = _userGroupRepository.GetList().To<UserGroup, UserGroupDTO>();

            return res;
        }
    }
}
